/**
 * 版权所有@2015 北京博创联动科技有限公司；未经许可，不得擅自复制、传播；
 */
package com.uptech.homer.beans.body;

import com.uptech.homer.beans.Message;
import com.uptech.homer.util.ByteUtils;
import org.springframework.data.annotation.Persistent;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

/**
 * GPS消息体
 *
 * @author lei
 */
public class RptGpsInfo extends Message.Body {
    public static final int _typeCode = 0x0003;
    private static final long serialVersionUID = 1L;
    /**
     * 时区；默认UTC
     */
    @Persistent
    private String timezone = "GMT";

    public RptGpsInfo() {
    }

    public RptGpsInfo(byte[] data) {
        this.setData(data);
    }

    public RptGpsInfo setTimezone(String timezone) {
        this.timezone = timezone;
        return this;
    }

    /**
     * 纬度
     *
     * @return
     */
    public byte[] getGpsLat() {
        return ByteUtils.getRange(data, 4, 8);
    }

    public double getGpsLatVal() {
        return ByteUtils.getUnsignedInt(data, 4, 8) / 1000000D;
    }

    /**
     * 经度
     *
     * @return
     */
    public byte[] getGpsLon() {
        return ByteUtils.getRange(data, 8, 12);
    }

    public double getGpsLonVal() {
        return ByteUtils.getUnsignedInt(data, 8, 12) / 1000000D;
    }

    /**
     * 速度
     *
     * @return
     */
    public byte[] getGpsSpeed() {
        return ByteUtils.getRange(data, 12, 13);
    }

    public int getGpsSpeedVal() {
        return ByteUtils.getUnsignedShort(data, 12, 13);
    }

    /**
     * 方向
     *
     * @return
     */
    public byte[] getGpsDirection() {
        return ByteUtils.getRange(data, 13, 15);
    }

    public int getGpsDirectionVal() {
        return ByteUtils.getUnsignedShort(data, 13, 15);
    }

    /**
     * 海拔高度
     *
     * @return
     */
    public byte[] getGpsAltitude() {
        return ByteUtils.getRange(data, 15, 17);
    }

    public int getGpsAltitudeVal() {
        return ByteUtils.getShort(data, 15, 17);
    }

    /**
     * 日期时间(UTC)
     *
     * @return
     */
    public byte[] getGpsDatetime() {
        return ByteUtils.getRange(data, 17, 23);
    }

    /**
     * 本地时间
     *
     * @return
     */
    public Date getGpsDatetimeVal() {

        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(0);

        int year = 2000 + ByteUtils.getUnsignedShort(data, 17, 18);
        int month = ByteUtils.getUnsignedShort(data, 18, 19) - 1;
        int date = ByteUtils.getUnsignedShort(data, 19, 20);
        int hour = ByteUtils.getUnsignedShort(data, 20, 21);
        int minute = ByteUtils.getUnsignedShort(data, 21, 22);
        int second = ByteUtils.getUnsignedShort(data, 22, 23);

        // UTC 时间
        if (ByteUtils.getUnsignedShort(data, 17, 18) > 0) {
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, month < 0 ? 0 : month > 11 ? 11 : month);
            c.set(Calendar.DAY_OF_MONTH, date < 1 ? 1 : date > 31 ? 31 : date);
            c.set(Calendar.HOUR_OF_DAY, hour < 0 ? 0 : hour > 23 ? 23 : hour);
            c.set(Calendar.MINUTE, minute < 0 ? 0 : minute > 59 ? 59 : minute);
            c.set(Calendar.SECOND, second < 0 ? 0 : second > 59 ? 59 : second);
        }

        // GMT to local TimeZone
        if ("GMT".equalsIgnoreCase(timezone)) {
            c.setTimeInMillis(c.getTimeInMillis() + TimeZone.getDefault().getOffset(System.currentTimeMillis()));
        }

        return c.getTime();
    }

    /**
     * 使用卫星颗数
     *
     * @return
     */
    public byte[] getUseSatellites() {
        return ByteUtils.getRange(data, 23, 24);
    }

    public int getUseSatellitesVal() {
        return ByteUtils.getUnsignedShort(data, 23, 24);
    }

    /**
     * 可视卫星颗数
     *
     * @return
     */
    public byte[] getGpsSatellites() {
        return ByteUtils.getRange(data, 24, 25);
    }

    public int getGpsSatellitesVal() {
        return ByteUtils.getUnsignedShort(data, 24, 25);
    }

    /**
     * GPS模块状态
     *
     * @return
     */
    public byte[] getGpsStatus() {
        return ByteUtils.getRange(data, 25, 26);
    }

    public int getGpsStatusVal() {
        return ByteUtils.getUnsignedShort(data, 25, 26);
    }

    public Map<String, Object> getRptGpsInfoMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("gpsLat", getGpsLatVal());
        map.put("gpsLon", getGpsLonVal());
        map.put("gpsSpeed", getGpsSpeedVal() + " KM/H");
        map.put("gpsDirection", getGpsDirectionVal());
        map.put("gpsAltitude", getGpsAltitudeVal() + " 米");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("gpsDatetime", formatter.format(getGpsDatetimeVal()));
        map.put("useSatellites", getUseSatellitesVal());
        map.put("gpsSatellites", getGpsSatellitesVal());
        map.put("gpsStatus", getGpsStatusVal());

        return map;

    }
}